/**
 * 
 */
package com.dsteiner.gwt.cafeteriasophia.client.model;

import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import com.dsteiner.gwt.cafeteriasophia.client.domain.Product;
import com.dsteiner.gwt.cafeteriasophia.client.domain.Purchase;
import com.dsteiner.gwt.cafeteriasophia.client.domain.Sale;
import com.dsteiner.gwt.cafeteriasophia.client.domain.Stock;
import com.dsteiner.widgets.client.PropertyChangeCollection;

public class ProductModel {
	private PropertyChangeCollection propertyChangeSupport = new PropertyChangeCollection(
			this);

	public void addPropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		propertyChangeSupport.registerPropertyChangeListener(propertyName,
				listener);
	}
	
	private Product product;

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		Product oldProduct = this.product;
		this.product = product;
		propertyChangeSupport.firePropertyChange("product", oldProduct, product);
	}
	
	private List<Sale> sales;

	public void addSale(Sale sale){
		List<Sale> newSales = new ArrayList<Sale>();
		newSales.addAll(sales);
		newSales.add(sale);
		setSales(newSales);
	}
	public void addPurchase(Purchase purchase){
		List<Purchase> newPurchases = new ArrayList<Purchase>();
		newPurchases.addAll(purchases);
		newPurchases.add(purchase);
		setPurchases(newPurchases);
	}
	public List<Sale> getSales() {
		return sales;
	}

	public void setSales(List<Sale> sales) {
		List<Sale> oldSale = this.sales;
		this.sales = sales;
		propertyChangeSupport.firePropertyChange("sales", oldSale, sales);
	}
	
	private List<Purchase> purchases;

	public List<Purchase> getPurchases() {
		return purchases;
	}

	public void setPurchases(List<Purchase> purchases) {
		List<Purchase> oldPurchases = this.purchases;
		this.purchases = purchases;
		propertyChangeSupport.firePropertyChange("purchases", oldPurchases, purchases);
	}
	
	private Stock stock;

	public Stock getStock() {
		return stock;
	}

	public void setStock(Stock stock) {
		Stock oldStock = this.stock;
		this.stock = stock;
		propertyChangeSupport.firePropertyChange("stock", oldStock, stock);
	}

	public void removeSale() {
		List<Sale> newSales = new ArrayList<Sale>();
		newSales.addAll(sales);
		newSales.remove(newSales.size()-1);
		setSales(newSales);
	}
}